package com.xnewtech.xplatform.swan.mtm.modular.modname.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.xnewtech.xplatform.swan.model.exp.RequestEmptyException;
import com.xnewtech.xplatform.swan.model.exp.ServiceException;
import com.xnewtech.xplatform.swan.mtm.modular.modname.entity.DmDevDbInfo;
import com.xnewtech.xplatform.swan.mtm.modular.modname.enums.MtmExceptionEnum;
import com.xnewtech.xplatform.swan.mtm.modular.modname.mapper.DmDevDbInfoMapper;
import com.xnewtech.xplatform.swan.mtm.modular.modname.service.DmDevDbInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (DmDevDbInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-09-22 15:20:14
 */
@Service("dmDevDbInfoService")
public class DmDevDbInfoServiceImpl implements DmDevDbInfoService {
    @Resource
    private DmDevDbInfoMapper dmDevDbInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param dbId 主键
     * @return 实例对象
     */
    @Override
    public DmDevDbInfo queryById(Long dbId) {
        return this.dmDevDbInfoMapper.queryById(dbId);
    }

    /*    *//**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     *//*
    @Override
    public List<DmDevDbInfo> queryAllByLimit(int offset, int limit) {
        return this.dmDevDbInfoDao.queryAllByLimit(offset, limit);
    }*/

    /**
     * 新增数据
     *
     * @param dmDevDbInfo 实例对象
     * @return 实例对象
     */
    @Override
    public DmDevDbInfo add(DmDevDbInfo dmDevDbInfo) {

        //判断不为空的参数是否接收到
        if (ObjectUtil.isEmpty(dmDevDbInfo)
                || ObjectUtil.isEmpty(dmDevDbInfo.getDbCode())
                || ObjectUtil.isEmpty(dmDevDbInfo.getVersion())
                || ObjectUtil.isEmpty(dmDevDbInfo.getDbStatus())) {
            throw new RequestEmptyException();
        }

        //判断数据库是否已经存在
        Map<Object, String> byDsIdAndDbName = this.findByDsIdAndDbName();
        for (Map.Entry<Object, String> entry : byDsIdAndDbName.entrySet()
        ) {
            if (dmDevDbInfo.getDbName() == entry.getValue() && dmDevDbInfo.getDsId() == entry.getKey() ){
                throw new ServiceException(MtmExceptionEnum.DATABASE_EXISTED);
            }
        }
        this.dmDevDbInfoMapper.insert(dmDevDbInfo);
        return dmDevDbInfo;
    }

    /**
     * 修改数据
     *
     * @param dmDevDbInfo 实例对象
     * @return 实例对象
     */
    @Override
    public DmDevDbInfo update(DmDevDbInfo dmDevDbInfo) {

        //判断是参数 是否为空
        if (ObjectUtil.isEmpty(dmDevDbInfo)
                || ObjectUtil.isEmpty(dmDevDbInfo.getDbId())
                || ObjectUtil.isEmpty(dmDevDbInfo.getDbCode())
                || ObjectUtil.isEmpty(dmDevDbInfo.getVersion())
                || ObjectUtil.isEmpty(dmDevDbInfo.getDbStatus())) {
            throw new RequestEmptyException();
        }

        //判断数据库是否存在,不存在回返信息，存在修改，改完后按照id显示
        DmDevDbInfo dmDevDbInfoById = this.queryById(dmDevDbInfo.getDbId());
        if (dmDevDbInfoById == null){
            throw new ServiceException(MtmExceptionEnum.NOT_EXISTED);
        }else {
            this.dmDevDbInfoMapper.update(dmDevDbInfo);
        }
        return this.queryById(dmDevDbInfo.getDbId());
    }

    @Override
    public Map<Object, String> findByDsIdAndDbName() {
        Map<Object, String> allByDbNameAndDsId = dmDevDbInfoMapper.findAllByDbNameAndDsId();
        return allByDbNameAndDsId;
    }
}